import { create, all } from 'mathjs'
import { formatMoney } from '@/utils/utils'

let math = create(all, {
  number: 'BigNumber',
  precision: 64
})

export default {
  data() {
    return {}
  },
  methods: {
    handleSummaries(param) { // 计算合计
      let { listData } = this
      let { columns } = param
      let sums = []
      let keys = ['baseSalary', 'assessmentSalary', 'postSalary', 'marketingSalary', 'subsidySalary', 'overtimeSalary', 'attendanceDeduction', 'allowanceSalary', 'teacherMealSalary', 'labourServiceSalary', 'otherDeduction', 'payableSalary', 'personalSocialSecurityTotalCost', 'personalSocialSecurityAdjust', 'enterpriseSocialSecurityTotalCost', 'securityCharge', 'beforeTaxSalary', 'personalIncomeTax', 'realSalary']
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        const values = listData.map(item => item[column.property] || 0)
        if (keys.indexOf(column.property) !== -1 && !values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            let value = Number(curr)
            if (!isNaN(value)) {
              let result = math.format(math.evaluate(`${prev} + ${curr}`))
              return result
            } else {
              return prev
            }
          }, 0)
          sums[index] = formatMoney(sums[index], 2, '¥')
        } else {
          sums[index] = 'N/A'
        }
      })
      return sums
    }
  }
}
